00:00
09:35

Hands-on Lab: Code Generation in Action

Estimated time needed: 45 minutes

Welcome to the hands-on lab code generation in action!

What is the code generation capability of generative AI?

Generative AI models can help you generate code snippets or programs based on the context you provide in natural language. These models can help you complete the code, generate another version of existing code, convert code from one programming language to another, and more. They also generate summaries and comments that you can use to improve related documentation.

In this lab, you will explore the capabilities of generative AI to generate code, including code generation and code conversion. You will use OpenAI's ChatGPT to generate the desired output.

Please don't be afraid if you have no prior coding or programming experience. You don't necessarily need to understand the code. This lab is more for demo purposes.

Learning Objectives

After completing this lab, you will be able to:

  • Explore the code generation capabilities of generative AI models
  • Prompt generative AI models to generate the Hello World code using ChatGPT
  • Use ChatGPT to generate the code in the desired programming language
  • Convert the code from one programming language to another using ChatGPT

ChatGPT Overview

ChatGPT is an AI chatbot developed by OpenAI based on large language models.
ChatGPT can help you with code-related tasks by providing explanations, pseudo-code, or algorithm suggestions. It can assist with answering programming questions, providing coding advice, and explaining coding concepts. While it's not a dedicated code generation tool, it can assist with programming-related tasks.

Exercise 1: Generate the Hello World code using ChatGPT

In this exercise, you will prompt ChatGPT to generate the Hello World code in the simple programming language C. You will then test and validate the code using CodeTester, an online code testing tool.

Step 1: Log in steps and the home page

  1. Click ChatGPT to launch it.

  2. Once launched, you will be required to sign up or log in.

Note: You can create a free account on ChatGPT and use GPT-3.5 features.

OpenAI landing page

a. To sign up, you will be required to enter your email ID and password. You will receive an automated verification email. After you click verification, you will need to enter your details and follow the steps to complete the sign up on the first login.

b. After signing up, you will reach the below login page.

OpenAI login page
  1. After logging in, you will view the OpenAI chatbot platform, as shown below.
ChatGPT home page

Step 2: Prepare the prompt and generate the code

  1. Consider a scenario or context for the Hello World code in C language.

  2. Write the prompt or statement in the Send a message field at the bottom of the ChatGPT screen.
    For example,

  1. 1
  1. Generate a simple C code that prints "Hello and welcome to the generative AI world!".

Note: For better results, include specifics and details about the context of the code you require.

  1. Now, click the arrow sign or press the enter key to send the message to the chatbot.

  2. You will receive a code response from ChatGPT for the desired C code, as shown below.

Code generation using ChatGPT

Note: You also get guidance on how to use the code.

Step 3: Test the generated code

Testing the generated code helps you validate the accuracy and reliability of the code. You can use any accessible online programming language compiler tool to test the code. Here, CodeTester has been used to test and validate the generated code.

CodeTester is a free online code testing tool that can help you run the code, assess it, and execute it on your site.

Let's begin with testing the generated code.

  1. Click CodeTester to launch it.

  2. Once launched, you will reach the CodeTester home page, as shown below.

CodeTester home page
  1. To reach the code testing page, you need to:

a. Select the Run Some Code option from the home page of CodeTester, as shown below.

CodeTester runner navigation

b. Alternatively, click CodeTester runner page.

  1. On the CodeTester testing page, you can see the test pane, the optional input box, and the grey color output box, as shown below.
CodeTester runner page
  1. Select the C language from the language drop-down option, as shown below.
CodeTester code language selection

Note: You can choose the code language for which you have generated the code.

  1. Now, go to ChatGPT to copy the generated code in step 1 using the Copy code option available in the upper right corner of the response field.
OpenAI code copy
  1. Go to the CodeTester testing page and delete the default code before pasting the copied code, as highlighted in the screenshot below.
CodeTester code testing pane
  1. Paste the copied code in the code test pane, as shown below.
CodeTester code pasting in the testing pane
  1. Click Run next to the language drop-down at the top to test and validate the code.
Codetester code run
  1. Review the results displayed in the lower box on the screen, as shown below.
CodeTester code test result
  1. You can try to generate various codes and functions using ChatGPT.

Note: You must validate the code generated through ChatGPT for factual accuracy and use the technology ethically and responsibly. You can also learn coding and programming as the model provides step-by-step explanations and guidance. However, you cannot generate large and complex code from scratch, as their training data set based on 2021 libraries may limit their capability.

Exercise 2: Create a code and convert it to another programming language using ChatGPT

Using generative AI foundation models, you can create the code in one programming language and describe the desired programming language in which you want to convert the code.

In this exercise, you will generate the code and convert it into another programming language using ChatGPT.

Step 1: Generate the code in JavaScript

  1. Consider a scenario or context to generate code in one programming language.

  2. Write the prompt or statement in the Send a message field at the bottom of the new chat window of ChatGPT.
    For example,

  1. 1
  1. Create a JavaScript code to generate a random number between 1 and 100.
  1. Now, click the arrow sign or press the enter key to send the message to the chatbot.

  2. You will receive a response from ChatGPT for the JavaScript code, as shown below.

OpenAI code generation

Step 2: Convert the code from JavaScript to Python

  1. Continue with the same chat window from step 1 in ChatGPT.

  2. Write the prompt or statement in the Send a message field at the bottom of the same chat window.
    For example,

  1. 1
  1. Convert the above code in Python.
  1. Now, click the arrow sign or press the enter key to send the message to the chatbot.

  2. You will receive a response from ChatGPT with the Python code, similar to the screenshot shown below.

OpenAI code conversion

Step 3: Test the converted code

You can validate the generated Python code by following the below steps:

  1. To test the code, launch the Python Online Compiler by clicking Programiz.

  2. Once launched, you can find the online Python compiler in the left ribbon bar. The left half of the home page represents the Python code pane, containing some default Python statements.

Python code compiler home page
  1. Delete the default Python statements before testing your code.
Python code testing pane
  1. Copy the generated code from the upper right corner of the ChatGPT response window, as shown in the screenshot below.
OpenAI code copy
  1. Paste the generated code in the Python code pane, as shown below.
Python code compiler code testing
  1. Click Run and you will receive the Python code result in the right pane of the screen, as shown below.
Python code compiler code result
  1. Click Run again to generate another random number.

  2. You can try to generate various codes and functions from ChatGPT and test the code using an online Python compiler.

Note: You must validate the code generated through ChatGPT for factual accuracy and use the technology ethically and responsibly. You can also learn coding and programming as the model provides step-by-step explanations and guidance. However, you cannot generate large and complex code from scratch, as their training data set based on 2021 libraries may limit their capability.

Summary:

Congratulations! You just completed the hands-on lab code generation in action.

In this lab, you explored some of the code generation capabilities of generative AI models, such as code generation and code conversion. You learned how to generate the desired code from generative AI models. Using ChatGPT, you learned to generate the code in the desired programming language and convert it into another.